import 'package:enjoy_plus_three/pages/house/house_list.dart';
import 'package:enjoy_plus_three/pages/house/location_list.dart';
import 'package:enjoy_plus_three/pages/login/index.dart';
import 'package:enjoy_plus_three/pages/notFound/index.dart';
import 'package:enjoy_plus_three/pages/profile/index.dart';
import 'package:enjoy_plus_three/utils/token.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../pages/notice/index.dart';
import '../pages/tab_bar_page.dart';

class RouterManager {
  // 普通路由
  static Map<String, WidgetBuilder> routes = {
    '/': (context) => const TabBarPage(),
    '/detail': (context) => const NoticeDetail(),
    '/login': (context) => const LoginPage(),
  };

  // 拦截路由
  static Route<dynamic> generateRoute(RouteSettings settings) {
    final tokenManager = TokenManager();
    final token = tokenManager.getToken() ?? '';
    // settings.name 访问的路径
    if (token.isEmpty && settings.name != '/login') {
      return MaterialPageRoute(builder: (context) => const LoginPage());
    }
    // 执行到这里的时候. token是存在的
    if (settings.name == '/profile') {
      return MaterialPageRoute(
          builder: (context) =>
              ProfilePage(userInfo: settings.arguments as Map));
    }

    if (settings.name == '/house_list') {
      return MaterialPageRoute(builder: (context) => const HouseList());
    }
    if (settings.name == '/location_list') {
      return MaterialPageRoute(builder: (context) => const LocationList());
    }

    // 统一处理(哪也不让跳) -> -> 实际应跳转到404页面
    return MaterialPageRoute(builder: (context) => const NotFound());
  }
}
